package com.tledu.production.controller.device;


import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.device.DeviceMaintain;
import com.tledu.production.service.device.DeviceMaintainService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/device")
@AllArgsConstructor
public class DeviceMaintainController {

    private final DeviceMaintainService deviceMaintainService;


    //分页联查
    @GetMapping("/device/maintain/list")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceMaintain>> DeviceMaintainList(Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceMaintain> list = deviceMaintainService.list();
        // 通过这个api获取pageInfo
        PageInfo<DeviceMaintain> page = PageInfo.of(list);
//        throw new CustomException("测试页面报错");
        return new AjaxResult<>(page);
    }

    //添加
    @PostMapping("/device/maintain/add")
    @ResponseBody
    public AjaxResult<Integer> DeviceMaintainAdd(DeviceMaintain deviceMaintain) {

        return new AjaxResult<>(deviceMaintainService.insert(deviceMaintain));
    }

    //编辑
    @PostMapping("/device/maintain/update")
    @ResponseBody
    public AjaxResult<Integer> DeviceMaintainUpdate(DeviceMaintain deviceMaintain) {

        return new AjaxResult<>(deviceMaintainService.update(deviceMaintain));
    }

    //删除
    @GetMapping("/device/maintain/delete")
    @ResponseBody
    public JSONObject delete(String deviceMaintainId) {
        int delete = deviceMaintainService.delete(deviceMaintainId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", delete);
        return jsonObject;
    }

    //按不同字段搜索
    @GetMapping("/device/maintain/search")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceMaintain>> DeviceMaintainSearchList(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceMaintain> list = deviceMaintainService.search(type, searchText);
        // 通过这个api获取pageInfo
        PageInfo<DeviceMaintain> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }



    //富文本默认值
    @RequestMapping("/device/maintain/editText/{deviceMaintainId}")
    public String edit(@PathVariable String deviceMaintainId, Model model) {
        String text = deviceMaintainService.findEdit(deviceMaintainId);
        model.addAttribute("editText",text);
        return "device/maintain-rich-text";
    }

    //富文本保存
    @RequestMapping("/device/maintain/edsave")
    @ResponseBody
    public AjaxResult<Void> edsave(String deviceMaintainId,String note){
        DeviceMaintain deviceMaintain = deviceMaintainService.findById(deviceMaintainId);
        deviceMaintain.setNote(note);
        deviceMaintainService.update(deviceMaintain);
        return new AjaxResult();
    }


    //批量删除
    @GetMapping("/device/maintain/deleteAll")
    @ResponseBody
    public AjaxResult DeviceMaintainDeleteAll(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            deviceMaintainService.delete(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
}

